# @Version        : 1.0
# @Update Time    : 2025/8/24 19:20
# @File           : exceptions.py
# @IDE            : PyCharm
# @Desc           : 自定义异常类

from typing import Any, Dict, Optional


class BaseAppException(Exception):
    """项目基础异常类

    所有自定义异常都应继承此类，以保持异常体系的一致性

    Attributes:
        msg: 错误消息
        code: 错误代码
        data: 附加数据
    """

    def __init__(self, msg: str = "未知错误", code: int = 500, data: Optional[Any] = None) -> None:
        """初始化基础异常

        Args:
            msg: 错误消息
            code: 错误代码
            data: 附加数据
        """
        self.msg = msg
        self.code = code
        self.data = data
        super().__init__(msg)

    def to_dict(self) -> Dict[str, Any]:
        """将异常转换为字典

        Returns:
            Dict[str, Any]: 包含异常信息的字典
        """
        return {"code": self.code, "msg": self.msg, "data": self.data}


class PermissionDeniedException(BaseAppException):
    """权限不足异常

    当用户没有足够权限执行操作时抛出
    """

    def __init__(self, msg: str = "权限不足", code: int = 403, data: Optional[Any] = None) -> None:
        """初始化权限不足异常

        Args:
            msg: 错误消息
            code: 错误代码
            data: 附加数据
        """
        super().__init__(msg, code, data)


class NotFoundException(BaseAppException):
    """资源不存在异常

    当请求的资源不存在时抛出
    """

    def __init__(self, msg: str = "资源不存在", code: int = 404, data: Optional[Any] = None) -> None:
        """初始化资源不存在异常

        Args:
            msg: 错误消息
            code: 错误代码
            data: 附加数据
        """
        super().__init__(msg, code, data)


class ValidationException(BaseAppException):
    """数据验证异常

    当输入数据验证失败时抛出
    """

    def __init__(self, msg: str = "数据验证失败", code: int = 400, data: Optional[Any] = None) -> None:
        """初始化数据验证异常

        Args:
            msg: 错误消息
            code: 错误代码
            data: 附加数据
        """
        super().__init__(msg, code, data)


class BusinessException(BaseAppException):
    """业务异常

    当业务逻辑出现异常时抛出
    """

    def __init__(self, msg: str = "业务处理异常", code: int = 500, data: Optional[Any] = None) -> None:
        """初始化业务异常

        Args:
            msg: 错误消息
            code: 错误代码
            data: 附加数据
        """
        super().__init__(msg, code, data)
